import numpy


class PLSDARequestBody:

    def __init__(self, xfile, yfile, ppmfile, thread_path):
        # 构建PLSDA模型的必须元素
        self.xfile = xfile
        self.yfile = yfile
        self.ppmfile = ppmfile
        self._ncomps = 2

        # 需要减一匹配习惯
        self._ycol = 0
        self.test_type1 = 1
        self.test_type2 = 2

        self._scaling_type = 0
        self.thread_path = thread_path
        self._dpi = 1000

        # score_plot中需要的元素
        self.plot_title = None
        # 需要减去一符合输入维数的习惯
        self._score_plot_comps_value1 = 0
        # 需要减去一符合输入维数的习惯
        self._score_plot_comps_value2 = 1
        self.score_plot_comps = [self._score_plot_comps_value1, self._score_plot_comps_value2]
        self._discrete = False
        # 需要减去一符合输入维数的习惯
        self._discrete_color_col_num = None
        self._label_outliers = False
        self._outliers_index = None
        # 正则的
        self._orthogonal_component = 1

        # scree_plot中需要的元素
        self._scree_total_comps = 10

        # outlier中需要的元素
        self._outlier_comps = None
        self._outlier_measure = "T2"
        self._outlier_alpha = 0.05

        # cv_plot中需要的元素
        self._cv_repeats = 5
        self._cv_total_comps = 10

        # permutation_test中需要的元素
        self._permutation_test_epochs = 1000
        # 测试的np保存的位置
        self._permutation_test_npy = None
        self._permutation_test_metric = "AUC"
        # 是否已经进行测试
        self._use_origin_permut = False

        # plot_model_parameters中需要的元素
        self._model_parameters = "w"
        self._model_comp = 1
        self._model_cross_val = False
        self._model_sigma = 2
        self._model_plottype = "spectra"

        # PLSR需要的元素


    @property
    def ncomps(self):
        return self._ncomps

    @ncomps.setter
    def ncomps(self, n):
        self._ncomps = int(n)

    @property
    def scaling_type(self):
        return self._scaling_type

    @scaling_type.setter
    def scaling_type(self, n):
        self._scaling_type = float(n)

    @property
    def dpi(self):
        return self._dpi

    @dpi.setter
    def dpi(self, n):
        self._dpi = int(n)

    @property
    def ycol(self):
        return self._ycol

    @ycol.setter
    def ycol(self, value):
        self._ycol = int(value) - 1

    @property
    def orthogonal_component(self):
        return self._orthogonal_component

    @orthogonal_component.setter
    def orthogonal_component(self, n):
        self._orthogonal_component = int(n)

    @property
    def score_plot_comps_value1(self):
        return self._score_plot_comps_value1

    @score_plot_comps_value1.setter
    def score_plot_comps_value1(self, n):
        self._score_plot_comps_value1 = int(n) - 1
        self.score_plot_comps = [self._score_plot_comps_value1, self._score_plot_comps_value2]

    @property
    def score_plot_comps_value2(self):
        return self._score_plot_comps_value2

    @score_plot_comps_value2.setter
    def score_plot_comps_value2(self, n):
        self._score_plot_comps_value2 = int(n) - 1
        self.score_plot_comps = [self._score_plot_comps_value1, self._score_plot_comps_value2]

    @property
    def discrete(self):
        return self._discrete

    @discrete.setter
    def discrete(self, value):
        self._discrete = bool(value)

    @property
    def discrete_color_col_num(self):
        return self._discrete_color_col_num

    @discrete_color_col_num.setter
    def discrete_color_col_num(self, value):
        if self._discrete is True:
            self._discrete_color_col_num = int(value) - 1

    @property
    def label_outliers(self):
        return self._label_outliers

    @label_outliers.setter
    def label_outliers(self, value):
        self._label_outliers = bool(value)

    @property
    def use_origin_permut(self):
        return self._use_origin_permut

    @use_origin_permut.setter
    def use_origin_permut(self, value):
        self._use_origin_permut = bool(value)

    @property
    def outliers_index(self):
        return self._outliers_index

    @outliers_index.setter
    def outliers_index(self, value):
        if value != "None":
            self._outliers_index = self.str2intList(value)

    @property
    def scree_total_comps(self):
        return self._scree_total_comps

    @scree_total_comps.setter
    def scree_total_comps(self, value):
        self._scree_total_comps = int(value)

    @property
    def cv_repeats(self):
        return self._cv_repeats

    @cv_repeats.setter
    def cv_repeats(self, value):
        self._cv_repeats = int(value)

    @property
    def cv_total_comps(self):
        return self._cv_total_comps

    @cv_total_comps.setter
    def cv_total_comps(self, value):
        self._cv_total_comps = int(value)

    @property
    def outlier_comps(self):
        return self._outlier_comps

    @outlier_comps.setter
    def outlier_comps(self, value):
        if value != "None":
            self._outlier_comps = int(value)

    @property
    def outlier_measure(self):
        return self._outlier_measure

    @outlier_measure.setter
    def outlier_measure(self, value):
        self._outlier_measure = value

    @property
    def outlier_alpha(self):
        return self._outlier_alpha

    @outlier_alpha.setter
    def outlier_alpha(self, value):
        self._outlier_alpha = float(value)

    @property
    def permutation_test_epochs(self):
        return self._permutation_test_epochs

    @permutation_test_epochs.setter
    def permutation_test_epochs(self, n):
        self._permutation_test_epochs = int(n)

    @property
    def use_origin_permut(self):
        return self._use_origin_permut

    @use_origin_permut.setter
    def use_origin_permut(self, n):
        self._use_origin_permut = bool(n)

    @property
    def permutation_test_npy(self):
        return self._permutation_test_npy

    @permutation_test_npy.setter
    def permutation_test_npy(self, n):
        if n == "None":
            self._permutation_test_npy = None
        else:
            self._permutation_test_npy = str(n)


    @property
    def permutation_test_metric(self):
        return self._permutation_test_metric

    @permutation_test_metric.setter
    def permutation_test_metric(self, n):
        self._permutation_test_metric = n

    def set_data(self, data):
        for key, value in data.items():  # 遍历数据字典
            if hasattr(self, key):  # 如果存在同名属性
                setattr(self, key, value)  # 则添加属性到对象中

    def str2intList(self, stringWaitToTrans):
        temp_list = stringWaitToTrans.strip(',').split(',')
        final_list = []
        for i in temp_list:
            final_list.append(int(i))
        return numpy.array(final_list)